<?php


namespace app\meetyou\controller;

use library\Controller;
use library\tools\Data;
use think\Db;

/**
 * 会员等级管理
 * Class Level
 * @package app\meetyou\controller
 */
class Level extends Controller {
    /**
     * 绑定数据表
     * @var string
     */
    protected $table = 'meetyou_member_level';

    /**
     * 会员等级列表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index() {
        $this->title = '会员等级管理';
        $this->_query($this->table)
            ->order('level asc')->page(false);
    }

    /**
     * 添加会员等级
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add() {
        $this->title = '添加会员等级';
        $this->applyCsrfToken();
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑会员等级
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit() {
        $this->title = '编辑会员等级';
        $this->applyCsrfToken();
        $this->_form($this->table, 'form');
    }

    /**
     * 等级表单处理
     * @param array $vo
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _form_filter(&$vo) {
        if ($this->request->isPost()) {
            if (isset($vo['level'])) {
                $level = intval($vo['level']);
                $id = intval($vo['id']);
                if (db($this->table)->where('level', $level)->where('id', '<>', $id)->find()) {
                    $this->error('等级不可重复');
                }
            }
        }
    }

    /**
     * 删除会员等级
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove() {
        $this->applyCsrfToken();
        $this->_delete($this->table);
    }
}